<?php

namespace App\Console\Commands;

use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\Warranty;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CarBrandsStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:CarBrandStat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        /*$start_date = $this->argument('start_date');
        $end_date = $this->argument('end_date');*/
        $this->statOldData(date('Y-m-d', strtotime('-1 day')), date('Y-m-d'));
        return 0;
    }

    public function statOldData(string $start_date, string $end_date)
    {
        $start_month = strtotime($start_date);
        $end_month = strtotime($end_date);
        for ($time = $start_month; $time < $end_month; $time += 86400) {
            $start_time = date('Y-m-d', $time);
            $end_time = date('Y-m-d', $time + 86400);
            $data = Warranty::where('approved_date', '>=', $start_time)
                ->where('approved_date', '<', $end_time)
                ->where('approved', 1)->get();
            if (empty($data) || $data->isEmpty()) {
                continue;
            }

            foreach ($data as $item) {
                if (empty($item->make_id) || empty($item->model_id)) {
                    continue;
                }

                if (is_integer($item->make_id)) {
                    $make_name = MakeMaster::where('id', $item->make_id)->select('simplified_chiness_value as name')->first();
                    if (empty($make_name) || empty($make_name->name)) {
                        continue;
                    }
                    $make_name = $make_name->name;
                } else {
                    $make_name = $item->make_id;
                }

                if (is_numeric($item->model_id)) {
                    $model_name = ModelMaster::where('id', $item->model_id)->select('simplified_chiness_value as name')->first();
                    if (empty($model_name) || empty($model_name->name)) {
                        $model_name = $item->model_id;
                    } else {
                        $model_name = $model_name->name;
                    }
                } else {
                    $model_name = $item->model_id;
                }

                $insertData = [
                    'dealer_id' => $item->user_id,
                    'dist_id'   => $item->distributor_id,
                    'car_brand' => $make_name,
                    'car_model' => $model_name,
                    'create_date' => date('Y-m-d', strtotime($item->installation_date)),
                ];
                DB::table('xpel_car_brand_stat')->insert($insertData);
            }
        }
    }
}
